package com.honghe.system.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.honghe.system.mapper.ContactRecordLogMapper;
import com.honghe.system.mapper.BankCustomerCasesMapper;
import com.honghe.system.domain.ContactRecordLog;
import com.honghe.system.domain.CustomerContactRecord;
import com.honghe.system.domain.BankCustomerCases;
import com.honghe.system.service.IContactRecordLogService;
import com.honghe.common.utils.StringUtils;

/**
 * 客户联系记录日志Service业务层处理
 * 
 * @author honghe
 */
@Service
public class ContactRecordLogServiceImpl implements IContactRecordLogService 
{
    @Autowired
    private ContactRecordLogMapper contactRecordLogMapper;
    
    @Autowired
    private BankCustomerCasesMapper bankCustomerCasesMapper;

    /**
     * 查询客户联系记录日志
     * 
     * @param logId 客户联系记录日志主键
     * @return 客户联系记录日志
     */
    @Override
    public ContactRecordLog selectContactRecordLogById(Long logId)
    {
        return contactRecordLogMapper.selectContactRecordLogById(logId);
    }

    /**
     * 查询客户联系记录日志列表
     * 
     * @param contactRecordLog 客户联系记录日志
     * @return 客户联系记录日志
     */
    @Override
    public List<ContactRecordLog> selectContactRecordLogList(ContactRecordLog contactRecordLog)
    {
        return contactRecordLogMapper.selectContactRecordLogList(contactRecordLog);
    }

    /**
     * 根据客户ID查询联系记录日志
     * 
     * @param caseId 客户ID
     * @return 客户联系记录日志集合
     */
    @Override
    public List<ContactRecordLog> selectContactRecordLogByCaseId(String caseId)
    {
        return contactRecordLogMapper.selectContactRecordLogByCaseId(caseId);
    }

    /**
     * 根据记录ID查询联系记录日志
     * 
     * @param recordId 记录ID
     * @return 客户联系记录日志集合
     */
    @Override
    public List<ContactRecordLog> selectContactRecordLogByRecordId(Long recordId)
    {
        return contactRecordLogMapper.selectContactRecordLogByRecordId(recordId);
    }

    /**
     * 新增客户联系记录日志
     * 
     * @param contactRecordLog 客户联系记录日志
     * @return 结果
     */
    @Override
    public int insertContactRecordLog(ContactRecordLog contactRecordLog)
    {
        return contactRecordLogMapper.insertContactRecordLog(contactRecordLog);
    }

    /**
     * 根据联系记录创建日志（添加记录）
     * 
     * @param record 联系记录
     * @param operationType 操作类型
     * @param operationBy 操作人
     * @return 结果
     */
    @Override
    public int createLogFromRecord(CustomerContactRecord record, String operationType, String operationBy)
    {
        if (record == null || record.getCaseId() == null) {
            return 0;
        }
        
        // 获取客户信息
        BankCustomerCases customer = null;
        try {
            // 将String类型的caseId转换为Long类型
            Long caseIdLong = StringUtils.isNotEmpty(record.getCaseId()) ? Long.parseLong(record.getCaseId()) : null;
            if (caseIdLong != null) {
                customer = bankCustomerCasesMapper.selectBankCustomerCasesByCaseId(caseIdLong);
            }
        } catch (Exception e) {
            // 转换失败或查询失败，忽略客户姓名
        }
        
        ContactRecordLog log = new ContactRecordLog();
        log.setRecordId(record.getRecordId());
        // 将String类型的caseId转换为Long类型
        if (StringUtils.isNotEmpty(record.getCaseId())) {
            try {
                log.setCaseId(Long.parseLong(record.getCaseId()));
            } catch (NumberFormatException e) {
                // 转换失败，设置为null
            }
        }
        log.setCustomerName(customer != null ? customer.getCustomerName() : "");
        log.setContactPerson(record.getContactPerson());
        log.setContactTime(record.getContactTime());
        log.setContactType(record.getContactType());
        log.setContactResult(record.getContactResult());
        log.setContactContent(record.getContactContent());
        log.setOperationType(operationType);
        log.setOperationBy(operationBy);
        log.setOperationTime(new Date());
        log.setCreateBy(operationBy);
        log.setRemark(String.format("操作人 %s %s了联系记录", operationBy, 
            "1".equals(operationType) ? "新增" : 
            "2".equals(operationType) ? "修改" : 
            "3".equals(operationType) ? "删除" : "操作"));
        
        return insertContactRecordLog(log);
    }

    /**
     * 批量新增客户联系记录日志
     * 
     * @param contactRecordLogList 客户联系记录日志列表
     * @return 结果
     */
    @Override
    public int batchInsertContactRecordLog(List<ContactRecordLog> contactRecordLogList)
    {
        return contactRecordLogMapper.batchInsertContactRecordLog(contactRecordLogList);
    }

    /**
     * 修改客户联系记录日志
     * 
     * @param contactRecordLog 客户联系记录日志
     * @return 结果
     */
    @Override
    public int updateContactRecordLog(ContactRecordLog contactRecordLog)
    {
        return contactRecordLogMapper.updateContactRecordLog(contactRecordLog);
    }

    /**
     * 删除客户联系记录日志信息
     * 
     * @param logId 客户联系记录日志主键
     * @return 结果
     */
    @Override
    public int deleteContactRecordLogById(Long logId)
    {
        return contactRecordLogMapper.deleteContactRecordLogById(logId);
    }

    /**
     * 批量删除客户联系记录日志
     * 
     * @param logIds 需要删除的客户联系记录日志主键
     * @return 结果
     */
    @Override
    public int deleteContactRecordLogByIds(Long[] logIds)
    {
        return contactRecordLogMapper.deleteContactRecordLogByIds(logIds);
    }
} 